package com.jtny.nytb.config.dictionary.controller;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jtny.component.entity.PageData;
import com.jtny.component.entity.WebResponse;
import com.jtny.component.exception.ServiceException;
import com.jtny.component.util.BeanUtils;
import com.jtny.nytb.config.dictionary.service.DictionaryBiz;
import com.jtny.nytb.config.dictionary.vo.*;
import com.jtny.nytb.config.excel.biz.ImportExcelBiz;
import com.jtny.nytb.config.excel.util.EasyExcelUtils;
import com.jtny.nytb.config.excel.util.ExcelSheetVO;
import com.jtny.nytb.config.utils.ExcelSheetUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 数据字典Controller类
 *
 * @author 智慧能源中心-lyq
 * @version 1.0
 * @package com.jtny.nytb.config.dictionary.controller
 * @fileName DictionaryController.java
 * @createTime 2023年09月01日 11:15
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@RestController
@Api(value = "数据字典管理", tags = "数据字典管理")
@RequestMapping("/config/dictionary")
@AllArgsConstructor
public class DictionaryController {

    private DictionaryBiz dictionaryBiz;
    private ImportExcelBiz importExcelBiz;
    private static final String OPER_SUCCESS_MESSAGE = "操作成功！";

    @ApiOperation("数据字典新增操作")
    @ApiImplicitParam(name = "dictionaryVO", value = "数据字典新增功能操作", required = true, paramType = "query", dataType = "DictionaryVO")
    @ApiOperationSupport(order = 1)
    @PostMapping("/save")
    @ResponseBody
    public WebResponse<?> saveDictionary(@RequestBody @Valid DictionaryInfoFromVO firstSaveDictionaryVO) {
        try {
            DictionaryVO insertDictionary = dictionaryBiz.saveDictionary(firstSaveDictionaryVO);
            return WebResponse.success(OPER_SUCCESS_MESSAGE, insertDictionary);
        } catch (ServiceException e) {
            return WebResponse.error(e);
        }

    }

    @ApiOperation("数据字典更新操作")
    @ApiImplicitParam(name = "dictionaryVO", value = "数据字典更新操作", required = true, paramType = "query", dataType = "DictionaryVO")
    @ApiOperationSupport(order = 2)
    @PutMapping("/update")
    @ResponseBody
    public WebResponse<?> updateDictionary(@RequestBody @Valid DictionaryVO dictionaryVO) {
        try {
            dictionaryBiz.updateDictionary(dictionaryVO);
            return WebResponse.success(OPER_SUCCESS_MESSAGE);
        } catch (ServiceException e) {
            return WebResponse.error(e);
        }
    }

    @ApiOperation("数据字典查看操作")
    @ApiImplicitParam(name = "id", value = "数据字典id", required = true, paramType = "path")
    @ApiOperationSupport(order = 3)
    @GetMapping("/detail/{id}")
    @ResponseBody
    public WebResponse<?> detailDictionary(@PathVariable("id") Long id) {
        DictionaryMessageVO dictionaryMessageVO = dictionaryBiz.detailDictionary(id);
        return WebResponse.success(dictionaryMessageVO);
    }

    @ApiOperation("数据字典分页查询操作")
    @ApiImplicitParam(name = "dictionaryPageQueryParamVO", value = "数据字典分页查询功能操作", required = true, paramType = "query", dataType = "DictionaryPageQueryParamVO")
    @ApiOperationSupport(order = 4)
    @PostMapping("/queryPage")
    @ResponseBody
    public WebResponse<?> pageQuery(@RequestBody @Valid DictionaryPageQueryParamVO dictionaryPageQueryParamVO) {
        PageData<DictionaryVO> dictionaryVOPageData = dictionaryBiz.queryPage(dictionaryPageQueryParamVO);
        return WebResponse.success(dictionaryVOPageData);
    }

    @ApiOperation("删除数据字典")
    @ApiImplicitParam(name = "id", value = "数据字典id", required = true, paramType = "path")
    @ApiOperationSupport(order = 5)
    @GetMapping("/delete/{id}")
    @ResponseBody
    public WebResponse<?> deleteDictionary(@PathVariable("id") Long id) {
        dictionaryBiz.deleteDictionary(id);
        return WebResponse.success(OPER_SUCCESS_MESSAGE);
    }

    @ApiOperation("禁用数据字典")
    @ApiImplicitParam(name = "id", value = "数据字典id", required = true, paramType = "path")
    @ApiOperationSupport(order = 6)
    @GetMapping("/disable/{id}")
    @ResponseBody
    public WebResponse<?> disableDictionary(@PathVariable("id") Long id) {
        dictionaryBiz.disableDictionary(id);
        return WebResponse.success(OPER_SUCCESS_MESSAGE);
    }

    @ApiOperation("启用数据字典")
    @ApiImplicitParam(name = "id", value = "数据字典id", required = true, paramType = "path")
    @ApiOperationSupport(order = 7)
    @GetMapping("/enable/{id}")
    @ResponseBody
    public WebResponse<?> enableDictionary(@PathVariable("id") Long id) {
        dictionaryBiz.enableDictionary(id);
        return WebResponse.success(OPER_SUCCESS_MESSAGE);
    }

    @ApiOperation("根据数据字典名称查询数据字典项信息")
    @ApiImplicitParam(name = "name", value = "数据字典名称", required = true, paramType = "path")
    @ApiOperationSupport(order = 8)
    @PostMapping("/queryDictionaryDataByName")
    @ResponseBody
    public WebResponse<?> queryDictionaryDataByName(@RequestParam(value = "name") String name) {
        List<DictionaryDataVO> dictionaryDataVOList = dictionaryBiz.queryDictionaryByName(name);
        if (dictionaryDataVOList != null && dictionaryDataVOList.size() > 0) {
            return WebResponse.success(OPER_SUCCESS_MESSAGE, dictionaryDataVOList);
        } else {
            return WebResponse.error("字典项为空！");
        }
    }
    @ApiOperation("根据数据字典名称查询数据字典项信息")
    @ApiImplicitParam(name = "name", value = "数据字典名称", required = true, paramType = "path")
    @ApiOperationSupport(order = 8)
    @PostMapping("/queryDictionaryDataByCode/{code}")
    @ResponseBody
    public WebResponse<?> queryDictionaryDataByCode(@PathVariable(value = "code") String code) {
        List<DictionaryDataVO> dictionaryDataVOList = dictionaryBiz.queryDictionaryByNameCode(code);
        if (dictionaryDataVOList != null && dictionaryDataVOList.size() > 0) {
            return WebResponse.success(OPER_SUCCESS_MESSAGE, dictionaryDataVOList);
        } else {
            return WebResponse.error("字典项为空！");
        }
    }

    @PostMapping("/export-template")
    @ApiOperation(value = "获取导入模板")
    public void exportTemplate(HttpServletResponse response) {
        EasyExcelUtils.exportExcelManySheet(response, "数据字典导入模版", ExcelSheetUtil.buildDictionaryExcelSheet());
    }

    @ApiOperation("导入数据字典")
    @ApiImplicitParam(name = "file", value = "数据字典excel文件", required = true, paramType = "query", dataType = "MultipartFile")
    @ApiOperationSupport(order = 9)
    @PostMapping("/import")
    @ResponseBody
    public WebResponse<?> importDictionary(@RequestParam(value = "file") MultipartFile file) {
        List<ExcelSheetVO> excelSheetList = ExcelSheetUtil.buildDictionaryExcelSheet();
        Map<Class<?>, List<?>> map = null;
        try {
            map = EasyExcelUtils.importExcelManySheet(file, excelSheetList);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        importExcelBiz.importDictionaryExcel(map, excelSheetList);
        return WebResponse.success(OPER_SUCCESS_MESSAGE);
    }

    @ApiOperation("导出数据字典")
    @ApiOperationSupport(order = 10)
    @PostMapping("/export")
    @ApiImplicitParam(name = "idList", value = "id列表", required = true, paramType = "body", dataType = "List<Long>")
    @ResponseBody
    public void exportDictionary(HttpServletResponse response, @RequestBody List<Long> idList) {
        DictionaryPageQueryParamVO dictionaryPageQueryParamVO = new DictionaryPageQueryParamVO();
        dictionaryPageQueryParamVO.setIdList(idList);
        dictionaryPageQueryParamVO.setPageSize(10000);
        PageData<DictionaryVO> dictionaryVOPageData = dictionaryBiz.queryPage(dictionaryPageQueryParamVO);
        List<ExcelDictionaryVO> toList = BeanUtils.copyToList(dictionaryVOPageData.getData(), ExcelDictionaryVO.class);
        EasyExcelUtils.exportExcel(response, "数据字典", toList, ExcelDictionaryVO.class);
    }


}
